package cn.pengpeng.day02.collectionLog;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class CollationLog extends TimerTask {

	@Override
	public void run() {
		/**
		 * 收集日志到hdfs上
		 * 
		 * 
		 * 
		 */
		System.out.println("采集任务启动");
		try {
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
			String dayStr = sdf.format(date);
			// 1.查看日志源中有哪些需要采集的文件
			File logDir = new File("D:\\testlog");
			File[] listFiles = logDir.listFiles(new FilenameFilter() {
				@Override
				public boolean accept(File dir, String name) {
					return name.startsWith("test.log.");
				}
			});
			
			// 2.将文件移动到隐式目录
			for (File file : listFiles) {
				// file.renameTo(new File("d:/toupload/"+file.getName()));
				FileUtils.moveFileToDirectory(file, new File("d:/toupload/"), true);

			}
			
			// 3. 将隐式目录中的文件逐个传输到hdfs上，并移动到备份目录中去
			FileSystem fs = FileSystem.get(new URI("hdfs://bigdata01:9000"), new Configuration(),"root");
			
			//判断这次采集的目标路径是否存在
			Path destDayPath = new Path("/collection/log/"+dayStr.substring(0, 13));
			boolean exists = fs.exists(destDayPath);
			if(!exists){
				fs.mkdirs(destDayPath);
			}
			
			//判断本次采集的数据的备份目录是否存在
			File backDir = new File("d:/backDir/"+dayStr);
			boolean exists2 = backDir.exists();
			if(!exists2){
				backDir.mkdirs();
			}
			
			String hostName = InetAddress.getLocalHost().getHostName();
			
			File tmpDir = new File("d:/toupload/");
			File[] listFiles2 = tmpDir.listFiles();
			for (File file : listFiles2) {
				//上传到hdfs上
				fs.copyFromLocalFile(new Path(file.getPath()), new Path(destDayPath,hostName+"_"+file.getName()+"_"+System.currentTimeMillis()));
				
				//备份
				FileUtils.moveFileToDirectory(file, backDir, true);
			}
			fs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
